import 'dart:math';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'cardView.dart';
import 'logic.dart' as logic;

class HowToPlay extends StatelessWidget {
  late final List<int> s1, s2, s3;
  late final List<List<int>> a1, a2, a3, a4, a5, p1;

  HowToPlay() : super() {
    final r = Random();
    final c1 = r.nextInt(3);
    final c2 = r.nextInt(3);
    s1 = <int>[c1, c1, c1];
    s2 = <int>[0, 1, 2]..shuffle();
    s3 = <int>[c2, c2, (c2 + r.nextInt(2) + 1) % 3]..shuffle();

    final c3 = r.nextInt(3); // Symbol
    final c4 = r.nextInt(3); // Colour
    a1 = ([0, 1, 2]..shuffle()).map<List<int>>((c) => [c3, c]).toList();
    a2 = ([0, 1, 2]..shuffle()).map<List<int>>((c) => [c, c4]).toList();
    final c5 = [0, 1, 2]..shuffle();
    a3 = ([0, 1, 2]..shuffle()).map<List<int>>((c) => [c, c5[c]]).toList();
    final c6 = r.nextInt(3);
    final c7 = [c6, c6, (r.nextInt(2) + c6 + 1) % 3]..shuffle();
    a4 = ([0, 1, 2]..shuffle()).map<List<int>>((c) => [c7[c], c]).toList();
    final c8 = r.nextInt(3);
    final c9 = [c8, c8, (r.nextInt(2) + c8 + 1) % 3]..shuffle();
    a5 = ([0, 1, 2]..shuffle()).map<List<int>>((c) => [c, c9[c]]).toList();

    final c10 = [
      [0, 1, 2]..shuffle(),
      [0, 1, 2]..shuffle(),
      [0, 1, 2]..shuffle(),
      [0, 1, 2]..shuffle(),
      [0, 1, 2]..shuffle(),
    ];

    p1 = c10[0]
        .map<List<int>>((c) => [c, c10[1][c], c10[2][c], c10[3][c], c10[4][c]])
        .toList();
  }

  Widget _tileRow(BuildContext context, Iterable<List<int>> attrs, bool omit,
          bool isSet) =>
      Row(
        mainAxisSize: MainAxisSize.min,
        children: attrs
            .map<Widget>((c) => Expanded(
                child: AspectRatio(
                    aspectRatio: 1,
                    child: Container(
                        margin: EdgeInsets.all(6),
                        width: 120,
                        height: 120,
                        child: CustomPaint(
                            painter: CardPainter(logic.Card.fromAttrs(c),
                                omit: omit))))))
            .followedBy([
          isSet
              ? Icon(
                  Icons.check,
                  color: Colors.lightGreen,
                  size: 50,
                )
              : Icon(
                  Icons.clear,
                  color: Colors.redAccent,
                  size: 50,
                )
        ]).toList(),
      );

  @override
  Widget build(BuildContext context) {
    final headStyle = Theme.of(context).textTheme.headline1;

    return Scaffold(
        appBar: AppBar(title: Text("玩法介绍")),
        body: ListView(
          padding: EdgeInsets.all(6),
          children: [
            Text(
              "这个游戏是为了练习右脑和左脑的同时工作，加强双脑沟通。",
              textAlign: TextAlign.justify,
            ),
            Text("游戏目标", style: headStyle),
            Text(
              "从网格中通过快速找到三组贴图获得积分",
              textAlign: TextAlign.justify,
            ),
            Text("关于卡片“相同”的概念", style: headStyle),
            Text("这是一个集合(因为颜色相同):"),
            _tileRow(context, s1.map((e) => [e, -1, 0, 0]), true, true),
            Text("\n这也是一个集合(虽然颜色不同):"),
            _tileRow(context, s2.map((e) => [e, -1, 0, 0]), true, true),
            Text("\n这不是一个集合(有两个颜色相同):"),
            _tileRow(context, s3.map((e) => [e, -1, 0, 0]), true, false),
            Text("没那么快", style: headStyle),
            Text("所有属性必须遵循“全部相同或全部不同”规则，以下这些是一个集合",
                textAlign: TextAlign.justify),
            _tileRow(context, a1, false, true),
            _tileRow(context, a2, false, true),
            _tileRow(context, a3, false, true),
            Text("\n颜色重复"),
            _tileRow(context, a4, false, false),
            Text("\n符号重复"),
            _tileRow(context, a5, false, false),
            Text("详细规则", style: headStyle),
            ...<Widget>[
              ...[
                "当找到一个集合时，这三个贴图被替换，游戏继续。",
                "每一盘1到30点，这取决于找到集合的时间。",
                "每一盘增加0到20秒的剩余游戏时间，这取决于找到集合的时间。",
                "如果使用了提示，找到该集合不会增加点数或时间。",
                "在练习模式没有时间限制(游戏永远不会结束)，每一盘得1分。"
              ].map((t) => Text(
                    t,
                    textAlign: TextAlign.justify,
                  )),
              Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("根据级别的不同，最多多达5个需判断的属性:", textAlign: TextAlign.justify),
                  ...[
                    "颜色 (" +
                        p1.map((c) => ["默认红", "绿", "蓝"][c[0]]).join(", ") +
                        ")",
                    "符号 (" +
                        p1.map((c) => ["曲线", "点", "竖线"][c[1]]).join(", ") +
                        ")",
                    "点数个数 (" + p1.map((c) => [1, 2, 3][c[2]]).join(", ") + ")",
                    "花纹样式 (" +
                        p1.map((c) => ["渐变", "条纹", "斑纹"][c[3]]).join(", ") +
                        ")",
                    "边角样式 (" +
                        p1.map((c) => ["三角", "直角", "圆"][c[4]]).join(", ") +
                        ")"
                  ].map((c) => Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(" • "),
                            Expanded(
                                child: Text(c, textAlign: TextAlign.justify))
                          ])),
                  _tileRow(context, p1, false, true),
                ],
              )
            ].asMap().entries.map((c) => Container(
                padding: EdgeInsets.only(top: c.key == 0 ? 0 : 10),
                child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(" ${c.key + 1}. "),
                      Expanded(
                        child: c.value,
                      )
                    ]))),
          ],
        ));
  }
}
